<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">点检任务名称：</div>
          <el-input
            v-model="taskName"
            clearable
            placeholder="请输入点检任务名称"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">点检人：</div>
          <el-select
            clearable
            v-model="personNo"
            placeholder="请选择"
            @change="queryPageList"
          >
            <el-option
              v-for="item in mixUserList"
              :key="item.personNo"
              :label="item.username"
              :value="item.personNo"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">点检时间：</div>
          <el-date-picker
            v-model="performStartTime"
            placeholder="选择点检时间"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="queryPageList()"
          />
        </div>
      </div>
    </template>
    <template v-slot:content>
      <e-table
        rowKey="taskId"
        ref="tableData"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :showSelectColumn="false"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="performStartTime"
          slot-scope="scoped"
          v-if="scoped.row.taskId"
        >
          <span>
            {{ scoped.row.startTime?scoped.row.startTime:'' }}
            <span
              v-if="scoped.row.startTime&&scoped.row.realEndTime"
            >至</span>
            {{ scoped.row.realEndTime?scoped.row.realEndTime:'' }}
          </span>
        </template>
        <template slot="view" slot-scope="scoped" v-if="scoped.row.taskId">
          <span
            class="table-btns-box"
            @click="ShowTaskResultDialog(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看结果</span>
          </span>
        </template>
        <template slot="imageList" slot-scope="scoped" v-if="scoped.row.taskId">
          <el-image
            style="width: auto; height: 40px"
            :src="scoped.row.imageList[0]"
            alt="点击查看全部图片"
            :preview-src-list="scoped.row.imageList"
          ></el-image>
        </template>

        <template slot="mark" slot-scope="scoped" v-if="scoped.row.taskId">
          <span
            class="table-btns-box"
            @click="() => {
              $alert(scoped.row.mark, '点检说明', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$message({
                    type: 'info',
                    message: `action: ${ action }`
                  });
                }
              });
            }"
          >
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>
      </e-table>
    </template>
    <template v-slot:dialog>
      <ShowTaskResultDialog
        ref="ShowTaskResultDialog"
        :show.sync="showDetailDialog"
        :taskId="taskId"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import temporaryTaskApi from "@/api/equip-inspection/temporary-task";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
import { getToken } from "@/utils/auth";
import ShowTaskResultDialog from "./components/show-task-result-diaog.vue";
import { maintenanceMixins } from "@/utils/maintenance-mixins";
export default {
  name: "TaskRecord",
  components: { BaseMainSlot, ShowTaskResultDialog },
  mixins: [mymixin, maintenanceMixins],
  data() {
    return {
      //点检时间
      performStartTime: "",
      showDetailDialog: false,
      isShowEquip: false,
      equipmentList: [], //设施列表
      taskName: "",
      loading: false,
      // 表单展示项
      tableLabel: [
        {
          label: "点检任务名称",
          param: "taskName",
          show: true,
        },
        {
          label: "点检执行人",
          param: "userName",
          show: true,
          notSearch: true,
        },
        {
          label: "点检时间",
          param: "performStartTime",
          type: "datetime_range",
          slotName: "performStartTime",
          show: true,
          notSearch: true,
          width: 400,
        },
        {
          label: "点检结果",
          param: "view",
          slotName: "view",
          show: true,
          notSearch: true,
        },
        {
          label: "点检图片",
          param: "imageList",
          slotName: "imageList",
          show: true,
          notSearch: true,
        },
        {
          label: "点检说明",
          param: "mark",
          slotName: "mark",
          show: true,
          notSearch: true,
        },
      ],
      //  搜索条件:设备名称：
      equipmentCode: "",
      equipmentArr: [],
      //  搜索条件:执行人
      personNo: "",
      taskId: null,
      startTime: [],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    this.queryPageList();
    this.mixQueryUserList();
  },
  mounted() {},
  methods: {
    ShowTaskResultDialog(row) {
      this.showDetailDialog = true;
      this.taskId = row.taskId;
    },
    // 取消
    cancel() {
      this.showDetailDialog = false;
      this.$refs.tableData.setCurrentRow();
      this.$refs.tableData.clearSelection();
    },
    querySearch(queryString, cd) {
      var equipmentData = this.equipmentArr;
      let results = queryString
        ? equipmentData.filter(this.createFilter(queryString))
        : equipmentData;
      // 调用 callback 返回建议列表的数据
      cd(results);
    },
    createFilter(queryString) {
      return (equipmentData) => {
        return (
          equipmentData.value
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      var params = {
        page: this.currentPage,
        size: this.pageSize,
      };
      var condition = [
        { paramName: "status", value: "COMPLETED", sign: "equals" },
        { paramName: "overdue", value: false, sign: "equals" },
        {
          paramName: "orgainzationMark",
          value: getToken("organizationNo"),
          sign: "equals",
        },
      ];
      if (this.taskName || e?.taskName) {
        condition.push({
          paramName: "taskName",
          value: this.taskName || e?.taskName,
          sign: "like",
        });
      }
      if (this.equipmentCode || e?.equipmentCode) {
        condition.push({
          paramName: "equipmentCode",
          value: this.equipmentCode || e?.equipmentCode,
          sign: "equals",
        });
      }
      if (this.personNo || e?.personNo) {
        condition.push({
          paramName: "personNo",
          value: this.personNo || e?.personNo,
          sign: "equals",
        });
      }
      if (this.performStartTime || e?.performStartTime) {
        condition.push({
          paramName: "performStartTime",
          value: this.performStartTime || e?.performStartTime,
          sign: "gte",
        });
      }
      if (sessionStorage.farming) {
        condition.push({
          paramName: "farming",
          value: sessionStorage.farming,
          sign: "equals",
        });
      }
      if (condition.length > 0) {
        params["condition"] = encodeURIComponent(JSON.stringify(condition));
      }
      temporaryTaskApi.queryPageList(params).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.total;
          this.tableData = data.list;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.taskId) {
          return item.taskId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          temporaryTaskApi
            .delList({ taskIdList: [data.taskId] })
            .then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.queryPageList();
                this.resetActionBar();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      temporaryTaskApi
        .delList({ taskIdList: this.checkedIdList })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
            this.resetActionBar();
          }
        });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.taskId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.seach-box {
  display: flex;
  justify-content: flex-start;
  .select-box-content {
    width: fit-content;
  }
}
</style>
